package com.lingying.caiwudailikehudaun.controller.wenJianJiaoJie;

import com.alibaba.fastjson.JSONObject;
import com.lingying.caiwudailikehudaun.domain.wenJianJiaoJie.WenJianJiaoJieBean;
import com.lingying.caiwudailikehudaun.domain.wuPinJiaoJie.UploadFileBean;
import com.lingying.caiwudailikehudaun.mapper.wenJianJiaoJie.WenJianJiaoJieMapper;
import com.lingying.caiwudailikehudaun.service.wenJianJiaoJie.WenJianJiaoJieService;
import com.lingying.caiwudailikehudaun.utils.FileType;
import com.lingying.caiwudailikehudaun.utils.OSSUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/wenJinaJiaoJie")
public class WenJianJiaoJieController {

    @Resource
    HttpServletRequest httpServletRequest;

    @Resource
    WenJianJiaoJieMapper wenJianJiaoJieMapper;

    @Resource
    WenJianJiaoJieService wenJianJiaoJieService;

    /**
     * 文件上传  桑满20210607
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFile")
    public Object uploadFile(
            @RequestParam("file") MultipartFile file,
            HttpServletResponse response,
            HttpServletRequest request
    ) throws Exception{

//        System.err.println(file);
        JSONObject object=new JSONObject();
        //获取文件原始名
        String fileName = file.getOriginalFilename();
        //生成随即名，防止文件覆盖
        fileName= UUID.randomUUID().toString().replace("-","");
        //获取文件后缀名
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);

        //文件路径
        String filePath = new OSSUtil().uploadDocument(file,suffix);
        //将文件信息装进数据库中
        UploadFileBean uploadFileBean = new UploadFileBean();
        uploadFileBean.setType(FileType.fileType(fileName));
        uploadFileBean.setFileName(file.getOriginalFilename());
        uploadFileBean.setCreateTime(new Date());
        uploadFileBean.setRandomName(fileName+""+suffix);
        uploadFileBean.setFilePath(filePath);
        Integer count = wenJianJiaoJieService.uploadFile(uploadFileBean);
        System.err.println(uploadFileBean.getUploadFileId());
        Integer uploadFileId = uploadFileBean.getUploadFileId();

//        PdfToImg.pdf2Image("D:\\springcloud--天气微服务文档07.pdf","D:\\1",1000);
//        List<String> imageList = PdfToImg.pdfToImagePath("D:\\"+file.getOriginalFilename());
//        Iterator<String> iterator = imageList.iterator();
//        while(iterator.hasNext()){
//            System.out.println(iterator.next());
//
//        }
        if(count>0){
            object.put("code",0);
            object.put("msg","上传成功");
            object.put("fileId",uploadFileBean.getUploadFileId());
            object.put("filePath",filePath);
            object.put("fileName",file.getOriginalFilename());
        }else{
            object.put("code",500);
            object.put("msg","上传失败");
        }

        return object;
    }

    /**
     * 添加文件交接  桑满20210607
     * @param wenJianJiaoJieBean
     * @return
     */
    @PostMapping("/addWenJianJiaoJie")
    public Object addWenJianJiaoJie(@RequestBody WenJianJiaoJieBean wenJianJiaoJieBean){
        JSONObject object=new JSONObject();
        //从session中拿到当前选中的公司信息
        Integer zuoZhangShenBaoId = (Integer) httpServletRequest.getSession().getAttribute("zuoZhangShenBaoId");
        String companyName = (String) httpServletRequest.getSession().getAttribute("companyName");
        Integer daiLiShangCid = (Integer) httpServletRequest.getSession().getAttribute("daiLiShangCid");
        String userName = (String) httpServletRequest.getSession().getAttribute("userName");
        String phone = (String) httpServletRequest.getSession().getAttribute("phone");
        Integer jiGouId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        //将信息添加进对象
        wenJianJiaoJieBean.setZuoZhangShenBaoId(zuoZhangShenBaoId);
        wenJianJiaoJieBean.setCompanyName(companyName);
        wenJianJiaoJieBean.setDaiLiShangCid(daiLiShangCid);
        wenJianJiaoJieBean.setTiJiaoRenName(userName);
        wenJianJiaoJieBean.setTiJiaoRenPhone(phone);
        wenJianJiaoJieBean.setJiGouId(jiGouId);

        //添加文件交接
        Integer count = wenJianJiaoJieMapper.addWenJianJiaoJie(wenJianJiaoJieBean);

        //获取到前端传递的文件id 并修改文件表关联字段wenJianJiaoJieId
        String str = wenJianJiaoJieBean.getWenJianId();
        if(str!=null||str.equals("")){
            Integer counts = wenJianJiaoJieMapper.updateWuPinId(wenJianJiaoJieBean.getWenJianJiaoJieId(), Integer.valueOf(str),zuoZhangShenBaoId);
        }
        System.err.println("=====>>>>>"+count);
        if(count>0){
            object.put("code",0);
            object.put("msg","添加成功");
        }else{
            object.put("code",500);
            object.put("msg","添加失败");
        }

        return object;
    }

    @GetMapping("/queryWenJianJiaoJie")
    public Object queryWenJianJiaoJie(
            @RequestParam(value = "xiangQing",required = false)String XiangQing,
            @RequestParam(value = "page",defaultValue = "0")Integer page,
            @RequestParam(value = "limit",defaultValue = "10")Integer limit
    ){
        JSONObject object=new JSONObject();
        //拿到数据当前页
        Integer pages=(page-1)*limit;
        //获取到当前登录人的手机号，并作为查询条件查询当前登录人提交的文件交接
        String phone = (String) httpServletRequest.getSession().getAttribute("phone");
        Integer zuoZhangShenBaoId = (Integer) httpServletRequest.getSession().getAttribute("zuoZhangShenBaoId");
        //数据查询
        List<WenJianJiaoJieBean> wenJianJiaoJieList = wenJianJiaoJieMapper.queryWenJianJiaoJie(phone,zuoZhangShenBaoId,XiangQing,pages,limit);
        //数据总条数
        Integer count = wenJianJiaoJieMapper.queryWenJianJiaoJieCount(phone,zuoZhangShenBaoId,XiangQing);
        //校验是否查询成功
        if(wenJianJiaoJieList!=null){
            object.put("code",0);
            object.put("msg","查询成功");
            object.put("data",wenJianJiaoJieList);
            object.put("count",count);
        }else{
            object.put("code",500);
            object.put("msg","查询失败");
        }
        return object;
    }
}



















